package com.dyna.demo.device.mapper;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dyna.core.utils.ObjectUtil;
import com.dyna.demo.device.domain.entity.DcDevicesInfo;
import com.dyna.demo.device.domain.entity.DeviceInfo;
import com.dyna.demo.device.domain.req.DcDeviceInfoReq;
import com.dyna.mapper.BaseMapperX;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface DeviceInfoMapper extends BaseMapperX<DcDevicesInfo> {

    default List<DcDevicesInfo> selectList(DcDeviceInfoReq req) {
        LambdaQueryWrapper<DcDevicesInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(req.getCode()), DcDevicesInfo::getCode, req.getCode());
        queryWrapper.eq(StrUtil.isNotBlank(req.getName()), DcDevicesInfo::getName, req.getName());
        queryWrapper.eq(ObjectUtil.isNotNull(req.getStatus()), DcDevicesInfo::getStatus, req.getStatus());
        queryWrapper.eq(ObjectUtil.isNotNull(req.getType()), DcDevicesInfo::getType, req.getType());
        queryWrapper.ge(ObjectUtil.isNotNull(req.getStartTime()), DcDevicesInfo::getCreateTime, req.getStartTime());
        queryWrapper.le(ObjectUtil.isNotNull(req.getEndTime()), DcDevicesInfo::getUpdateTime, req.getEndTime());
        return selectList(queryWrapper);
    }

    /**
     * 查询所有设备信息
     * @return List<DeviceInfo>
     */
    @Select("select code,name,longitude,latitude from demo.dc_devices_info where del_flag = 0")
    List<DeviceInfo> queryAllDeviceInfo();

    /**
     * 根据设备code查询设备名称
     * @return 设备名称
     */
    @Select("select name from demo.dc_devices_info where del_flag = 0 and code = #{code}")
    String queryDeviceInfoByCode(@Param("code") String code);

}